using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class EntityValueDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public EntityValueDA()
		{
		}

		public EntityValueDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the EntityValue table.
		/// </summary>
		public  int Insert(EntityValue entityValue)
		{
			ValidationUtility.ValidateArgument("entityValue", entityValue);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", entityValue.TenantID),
				new SqlParameter("@EntityVal", entityValue.EntityVal),
				new SqlParameter("@EntityID", entityValue.EntityID)
			};

			entityValue.EntityValueID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "EntityValueInsert", parameters));
			return entityValue.EntityValueID;
		}

		/// <summary>
		/// Deletes a record from the EntityValue table by a foreign key.
		/// </summary>
		public  int DeleteAllByEntityID(int entityID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EntityID", entityID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "EntityValueDeleteAllByEntityID", parameters));
		}

		/// <summary>
		/// Deletes a record from the EntityValue table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "EntityValueDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects all records from the EntityValue table.
		/// </summary>
		public  List<EntityValue> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "EntityValueSelectAll", parameters))
			{
				List<EntityValue> entityValueList = new List<EntityValue>();
				while (dataReader.Read())
				{
					EntityValue entityValue = MakeEntityValue(dataReader);
					entityValueList.Add(entityValue);
				}

				return entityValueList;
			}
		}

		/// <summary>
		/// Selects all records from the EntityValue table by a foreign key.
		/// </summary>
		public  List<EntityValue> SelectAllByEntityID(int entityID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@EntityID", entityID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "EntityValueSelectAllByEntityID", parameters))
			{
				List<EntityValue> entityValueList = new List<EntityValue>();
				while (dataReader.Read())
				{
					EntityValue entityValue = MakeEntityValue(dataReader);
					entityValueList.Add(entityValue);
				}

				return entityValueList;
			}
		}

		/// <summary>
		/// Selects all records from the EntityValue table by a foreign key.
		/// </summary>
		public  List<EntityValue> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "EntityValueSelectAllByTenantID", parameters))
			{
				List<EntityValue> entityValueList = new List<EntityValue>();
				while (dataReader.Read())
				{
					EntityValue entityValue = MakeEntityValue(dataReader);
					entityValueList.Add(entityValue);
				}

				return entityValueList;
			}
		}

		/// <summary>
		/// Creates a new instance of the EntityValue class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  EntityValue MakeEntityValue(SqlDataReader dataReader)
		{
			EntityValue entityValue = new EntityValue();
			entityValue.EntityValueID = SqlClientUtility.GetInt32(dataReader, "EntityValueID", 0);
			entityValue.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			entityValue.EntityVal = SqlClientUtility.GetString(dataReader, "EntityVal", String.Empty);
			entityValue.EntityID = SqlClientUtility.GetInt32(dataReader, "EntityID", 0);

			return entityValue;
		}

		#endregion
	}
}
